package spring.boot.admin.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import spring.boot.admin.model.*;
import spring.boot.admin.service.SysRoleMenuService;
import spring.boot.admin.service.SysRoleService;

import java.util.List;

/**
 * 角色管理  控制器
 */
@Controller
@RequestMapping("/sys/role")
public class SysRoleController {


    @Autowired
    private SysRoleService sysRoleService;


    @Autowired
    private SysRoleMenuService sysRoleMenuService;


    /**
     * 跳转到角色管理页面
     */
    @GetMapping("to/list")
    @PreAuthorize("hasRole('SYS_ROLE_LIST')")
    public String toList() {
        return "sys/role/list";
    }


    /**
     * 分页查询角色
     */
    @PostMapping("list")
    @ResponseBody
    @PreAuthorize("hasRole('SYS_ROLE_LIST')")
    public IPage<SysRole> list(Integer page, Integer rows) {
        if (page == null) {
            page = 1;
        }
        if (rows == null) {
            rows = 10;
        }
        return sysRoleService.pageList(page, rows);
    }


    /**新增角色*/
    @PostMapping("insert")
    @ResponseBody
    @PreAuthorize("hasRole('SYS_ROLE_ADD')")
    public String insert(@RequestParam(required = false) String name,
                         @RequestParam(required = false) Integer enable) {
        if (name == null || enable == null) {
            return "-1";
        }
        return sysRoleService.insert(name, enable);
    }


    /**
     * 根据主键删除角色
     */
    @PostMapping("deleteById")
    @ResponseBody
    @PreAuthorize("hasRole('SYS_ROLE_DELETE')")
    public String deleteById(@RequestParam(required = false) Long id) {
        if (id == null) {
            return "-1";
        }
        return sysRoleService.deleteById(id);

    }


    /**
     * 根据主键修改角色
     */
    @PostMapping("update")
    @ResponseBody
    @PreAuthorize("hasRole('SYS_ROLE_UPDATE')")
    public String update(@RequestParam(required = false) Long id,
                         @RequestParam(required = false) String name,
                         @RequestParam(required = false) Integer enable) {
        if (id == null || name == null || enable == null) {
            return "-1";
        }
        return sysRoleService.updateRoleById(id, name, enable);
    }


    /**
     * 查询所有菜单tree，返回与角色的关联关系
     */
    @PostMapping("roleMenu")
    @ResponseBody
    @PreAuthorize("hasRole('SYS_ROLE_MENU_UPDATE')")
    public List<SysMenu> roleMenu(Long roleId) {
        if (roleId == null) {
            return null;
        }
        return sysRoleService.roleMenu(roleId);
    }


    /**
     * 修改角色对应的菜单
     */
    @PostMapping("updateRoleMenu")
    @ResponseBody
    @PreAuthorize("hasRole('SYS_ROLE_MENU_UPDATE')")
    public String updateRoleMenu(@RequestParam(required = false) Long roleId,
                                 @RequestParam(required = false) Long[] menuIds) {
        if (roleId == null || menuIds == null) {
            return "-1";
        }
        return sysRoleMenuService.updateRoleMenu(roleId, menuIds);
    }




}
